<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomId">
        <el-input
          v-model="queryParams.roomId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否完成" prop="isExist">

        <el-select v-model="queryParams.isExist">

          <el-option 
          v-for="option in options"
          :label="option.label" 
          :value="option.value"
          :key="option.value" 
        />
        
        </el-select>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

 

    <el-table v-loading="loading" :data="bookList">
      
      <el-table-column label="用户id" align="center" prop="customerId" />
      <el-table-column label="入住时间" align="center" prop="checkinTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkinTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退房时间" align="center" prop="checkoutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkoutTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="房间号" align="center" prop="roomId" />
      <el-table-column label="服务需求" align="center" prop="serviceRequestContent" />
      <el-table-column label="是否完成" align="center" prop="isExist" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="checkin(scope.row)"
          >登记入住</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-error"
            @click="cancelThisBook(scope.row)"
          >取消预约</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listBook, delBook, addBook, updateBook,doCheckin,cancelBook } from "@/api/hotel/book";

export default {
  name: "Book",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: undefined,
        checkinTime: undefined,
        checkoutTime: undefined,
        roomId: undefined,
        serviceRequestContent: undefined,
        isExist : undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      options: [
        { label: '否', value: 'y' },
        { label: '是', value: 'n' },
        { label: '已取消', value: 'q' },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listBook(this.queryParams).then(response => {
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bookId: undefined,
        customerId: undefined,
        checkinTime: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        checkoutTime: undefined,
        roomId: undefined,
        serviceRequestContent: undefined,
        isExist:undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.bookId != null) {
            updateBook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addBook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bookIds = row.bookId || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + bookIds + '"的数据项？').then(() => {
        this.loading = true;
        return delBook(bookIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },

    checkin(row){
      this.form= row;

      if(this.form.isExist){

        doCheckin(this.form).then(response => {
                this.$modal.msgSuccess("登记成功");
                this.getList();
              }).finally(() => {
                this.cancel();
              });

      }
      else{
          this.$modal.msgSuccess("无法对已完成的预约操作");
      }
    },

    cancelThisBook(row){
      this.form=row;

      if(this.form.isExist){
      cancelBook(this.form).then(response => {
                this.$modal.msgSuccess("取消成功");
                this.getList();
              }).finally(() => {
                this.cancel();
              });
      }else{
          this.$modal.msgSuccess("无法对已完成的预约操作");
      }
      
    },

    statusFormat(row,column){
      if(row.isExist=="y"){
        return "否";
      }
      else if(row.isExist=="n"){
        return "是";
      }
      else if(row.isExist=="q"){
        return "已取消";
      }
    }
  }
};
</script>
